/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.modelo.Area;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;



/**
 *
 * @author Tecinfo
 */
public class Nova_area extends javax.swing.JFrame {
    
    int linha_selec = -1;

    /**
     * Creates new form Nova_area
     */
    public Nova_area() {
        initComponents();
 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cl_codigo = new javax.swing.JLabel();
        ct_codigoArea = new javax.swing.JTextField();
        jl_cad_area = new javax.swing.JLabel();
        bt_cad_area = new javax.swing.JButton();
        bt_edit_area = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tb_area = new javax.swing.JTable();
        bt_limpar = new javax.swing.JButton();
        bt_remover = new javax.swing.JButton();
        ct_abrevArea = new javax.swing.JTextField();
        ct_nomeArea = new javax.swing.JTextField();
        cl_nome = new javax.swing.JLabel();
        cl_abrev = new javax.swing.JLabel();

        cl_codigo.setText("Codigo:");

        jl_cad_area.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jl_cad_area.setText("Cadastrar Área");

        bt_cad_area.setText("Cadastrar");
        bt_cad_area.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cad_areaActionPerformed(evt);
            }
        });

        bt_edit_area.setText("Editar");
        bt_edit_area.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_edit_areaActionPerformed(evt);
            }
        });

        tb_area.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Abreviação", "Codigo"
            }
        ));
        jScrollPane1.setViewportView(tb_area);

        bt_limpar.setText("Limpar");
        bt_limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limparActionPerformed(evt);
            }
        });

        bt_remover.setText("Remover");
        bt_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_removerActionPerformed(evt);
            }
        });

        cl_nome.setText("Nome:");

        cl_abrev.setText("Abrev:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_cad_area)
                        .addGap(10, 10, 10)
                        .addComponent(bt_limpar)
                        .addGap(10, 10, 10)
                        .addComponent(bt_edit_area)
                        .addGap(10, 10, 10)
                        .addComponent(bt_remover))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jl_cad_area, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cl_nome)
                                .addComponent(cl_abrev)
                                .addComponent(cl_codigo))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ct_nomeArea, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ct_abrevArea, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ct_codigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(40, 40, 40)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jl_cad_area, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cl_nome)
                            .addComponent(ct_nomeArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cl_abrev)
                            .addComponent(ct_abrevArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cl_codigo)
                            .addComponent(ct_codigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bt_cad_area)
                            .addComponent(bt_limpar)
                            .addComponent(bt_edit_area)
                            .addComponent(bt_remover))))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cad_areaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cad_areaActionPerformed

        if (linha_selec >= 0){

            tb_area.setValueAt(ct_nomeArea.getText (), linha_selec, 0);
            tb_area.setValueAt(ct_abrevArea.getText (), linha_selec, 1);
            tb_area.setValueAt(ct_codigoArea.getText (), linha_selec, 2);

            ct_codigoArea.setText("");
            ct_nomeArea.setText("");
            ct_abrevArea.setText("");

            JOptionPane.showMessageDialog(null, "Dados Atualizados com Sucesso!", "Cadastro Área", JOptionPane.INFORMATION_MESSAGE);

            linha_selec = -1;
            bt_cad_area.setText("Cadastrar");

        } else{
            try{  
            int CodigoArea = Integer.parseInt(ct_codigoArea.getText());
            Area nova_areaArea = new Area (ct_nomeArea.getText(), ct_abrevArea.getText(), CodigoArea);
            DefaultTableModel dtm = (DefaultTableModel) tb_area.getModel();
            Object[] opcoes = {ct_codigoArea.getText(), ct_abrevArea.getText(), ct_codigoArea.getText()};
            dtm.addRow(opcoes);

            ct_codigoArea.setText("");
            ct_nomeArea.setText("");
            ct_abrevArea.setText("");

            JOptionPane.showMessageDialog(null, "Cargo Cadastrado com Sucesso!", "Cadastro Cargo", JOptionPane.INFORMATION_MESSAGE);
        
                
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, "Digite o codigo corretamente");
            }
            }

    }//GEN-LAST:event_bt_cad_areaActionPerformed

    private void bt_edit_areaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_edit_areaActionPerformed

        linha_selec = tb_area.getSelectedRow();

        if (linha_selec <= -1){

            JOptionPane.showMessageDialog(null, "Selecione Uma Linha");

        } else{
            bt_cad_area.setText("Atualizar");

            ct_codigoArea.setText(tb_area.getValueAt(linha_selec, 0).toString());
            ct_abrevArea.setText(tb_area.getValueAt(linha_selec, 1).toString());
            ct_nomeArea.setText(tb_area.getValueAt(linha_selec, 2).toString());
            
        }
    }//GEN-LAST:event_bt_edit_areaActionPerformed

    private void bt_limparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limparActionPerformed

        ct_codigoArea.setText("");
        ct_abrevArea.setText("");
        ct_nomeArea.setText("");
    }//GEN-LAST:event_bt_limparActionPerformed

    private void bt_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_removerActionPerformed

        DefaultTableModel dtm = (DefaultTableModel) tb_area.getModel();

        int linha = tb_area.getSelectedRow();
        Object[] dados = {ct_codigoArea.getText()};

        dtm.removeRow(linha);
        JOptionPane.showMessageDialog(null, "Área removida com sucesso!", "Cadastro Área", JOptionPane.INFORMATION_MESSAGE);

    }//GEN-LAST:event_bt_removerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Nova_area.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Nova_area.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Nova_area.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Nova_area.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Nova_area().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cad_area;
    private javax.swing.JButton bt_edit_area;
    private javax.swing.JButton bt_limpar;
    private javax.swing.JButton bt_remover;
    private javax.swing.JLabel cl_abrev;
    private javax.swing.JLabel cl_codigo;
    private javax.swing.JLabel cl_nome;
    private javax.swing.JTextField ct_abrevArea;
    private javax.swing.JTextField ct_codigoArea;
    private javax.swing.JTextField ct_nomeArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jl_cad_area;
    private javax.swing.JTable tb_area;
    // End of variables declaration//GEN-END:variables
}
